package Server.DAO;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import VWorld.Avatar;
import VWorld.Room;

public class AvatarDao extends VWorldDao{
	
	/**
	 * Return the list of all avatars
	 * @return Avatar list
	 */
	public static Avatar[] getAll(){
		connection();
		Statement stm;
		ArrayList<Avatar> avatars = new ArrayList<Avatar>();
		try {
			stm = con.createStatement();
			
			stm.execute ("SELECT avatar_id, height, humor, sex, name, password, email, isAdmin, currentRoomId FROM Avatar"); 
			ResultSet rs = stm.getResultSet();
			boolean isAdmin;
			int i =0;
			while(rs.next()){

				
				if(rs.getInt("isAdmin")==0){
					isAdmin= false;
				}else{
					isAdmin= true;
				}
			
				Avatar avatar = new Avatar(rs.getInt("avatar_id"),
						VWorld.Height.from_int(rs.getInt("height")), 
						VWorld.Humor.from_int(rs.getInt("humor")), 
						VWorld.Sex.from_int(rs.getInt("sex")), 
						rs.getString("name"), 
						rs.getString("password"), 
						rs.getString("email"), 
						isAdmin, 
						rs.getInt("currentRoomId"),
						true
					);
				avatars.add(avatar);
				i++;
			}
			
		} catch (SQLException e) {
			System.out.println("getRoom(id)");
			e.printStackTrace();
		}
		Avatar[] av = new Avatar[avatars.size()];
		
		
		return avatars.toArray(av) ;
	}

	public static Avatar getById(int avatar_id) {
		connection();
		Statement stm;
		Avatar avatar=null;
		try {
			stm = con.createStatement();
			
			stm.execute ("SELECT avatar_id, height, humor, sex, name, password, email, isAdmin, currentRoomId FROM Avatar WHERE avatar_id='"+avatar_id+"'"); 
			ResultSet rs = stm.getResultSet();
			boolean isAdmin;
			
			rs.next();

				
				if(rs.getInt("isAdmin")==0){
					isAdmin= false;
				}else{
					isAdmin= true;
				}
			
			 avatar = new Avatar(rs.getInt("avatar_id"),
						VWorld.Height.from_int(rs.getInt("height")), 
						VWorld.Humor.from_int(rs.getInt("humor")), 
						VWorld.Sex.from_int(rs.getInt("sex")), 
						rs.getString("name"), 
						rs.getString("password"), 
						rs.getString("email"), 
						isAdmin, 
						rs.getInt("currentRoomId"),
						true
					);
			
			
			
		} catch (SQLException e) {
			System.out.println("erreur getavatar by id(id)");
			e.printStackTrace();
		}
		
		return avatar;
	}

}
